package cn.edu.ctbu.simplescoremanagementsystem.webapi;

import cn.edu.ctbu.simplescoremanagementsystem.constain.ResultEnum;
import cn.edu.ctbu.simplescoremanagementsystem.core.PageUtils;
import cn.edu.ctbu.simplescoremanagementsystem.domain.Student;
import cn.edu.ctbu.simplescoremanagementsystem.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import cn.edu.ctbu.simplescoremanagementsystem.vo.Result;
import cn.edu.ctbu.simplescoremanagementsystem.utils.ResultUtils;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {
    @Autowired
    StudentService studentService;

    @GetMapping("/findAll")
    public Result<List<Student>> getAll(){
        List<Student>students = studentService.findAll().getData();
        return ResultUtils.success(students);
    }

    @GetMapping("/get/{id}")
    public Result<List<Student>> getID(@PathVariable Integer id){
        Student student = studentService.findById(id).getData();
        student.setPassword("");//不返回密码
        return ResultUtils.success(student);
    }

    @PostMapping("/insert")
    public Result insert(Student student){
        studentService.insert(student);
        return ResultUtils.success();
    }

    @PutMapping("/update")
    public Result update(Student student){
       try{
           Student oldstudent = studentService.findById(student.getId()).getData();
           if(StringUtils.isEmpty(student.getPassword())) {
               student.setPassword(oldstudent.getPassword());
           }
       }catch (Exception e){
           log.error(e.getMessage());
           return ResultUtils.error(-1,"更新失败,该学生不存在");
       }
        studentService.update(student);
        return ResultUtils.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        try{
            studentService.Delete(id);
        }catch (Exception e){
            log.error(e.getMessage());
            return ResultUtils.error(ResultEnum.NOT_FIND);
        }
        return ResultUtils.success();
    }

    @GetMapping("/getByPage")
    public Result<PageUtils> getByPage(@RequestParam(value = "page",defaultValue = "0")Integer page,
                               @RequestParam(value = "size",defaultValue = "7")Integer size,
                               @RequestParam(value = "name",defaultValue = "")String name,
                               @RequestParam(value = "no",defaultValue = "")String no){
        Sort sort = Sort.by(Sort.Direction.ASC, "id");//排序
        Page<Student> studentPage;
        Pageable pageable = PageRequest.of(page,size,sort);
        if(StringUtils.isEmpty(name)&&StringUtils.isEmpty(no)){
            studentPage = studentService.getByPage(pageable);
        }else{
            Student student = new Student();
            ExampleMatcher matcher = null;
            if(!StringUtils.isEmpty(name)){
                student.setSname(name);
                matcher = ExampleMatcher.matching()
                        .withMatcher("sname",ExampleMatcher.GenericPropertyMatchers.contains());//模糊查询包含，即%{username}%
            }
            if(!StringUtils.isEmpty(no)){
                student.setSno(no);
                matcher = ExampleMatcher.matching()
                        .withMatcher("sno",ExampleMatcher.GenericPropertyMatchers.contains());//模糊查询包含，即%{username}%
            }


            Example<Student> example = Example.of(student,matcher);//模糊查询
            studentPage = studentService.getByPage(example,pageable);
        }
        PageUtils pageUtils = new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        return ResultUtils.success(pageUtils);
    }

    @GetMapping("/getByPage1")
    public PageUtils getByPage1(@RequestParam(value = "page",defaultValue = "0")Integer page,
                                           @RequestParam(value = "size",defaultValue = "7")Integer size,
                                           @RequestParam(value = "name",defaultValue = "")String name,
                                           @RequestParam(value = "no",defaultValue = "")String no){
        Sort sort = Sort.by(Sort.Direction.ASC, "id");//排序
        Page<Student> studentPage;
        if(StringUtils.isEmpty(name)&&StringUtils.isEmpty(no)){
            Pageable pageable = PageRequest.of(page,size,sort);
            studentPage = studentService.getByPage(pageable);
        }else{
            Student student = new Student();
            if(!StringUtils.isEmpty(name)){
                student.setSname(name);
            }
            if(!StringUtils.isEmpty(no)){
                student.setSno(no);
            }
            Pageable pageable = PageRequest.of(page,size,sort);
            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());//模糊查询包含，即%{username}%
            Example<Student> example = Example.of(student,matcher);//模糊查询
            studentPage = studentService.getByPage(example,pageable);
        }
        PageUtils pageUtils = new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        return pageUtils;
    }



}
